<?php

/**
 * ECSHOP 文章
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: article.php 16455 2009-07-13 09:57:19Z liubo $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$act = !empty($_GET['act']) ? $_GET['act'] : '';
//if ($_SESSION['user_id'] > 0)
//{
//	$smarty->assign('user_name', $_SESSION['user_name']);
//
//}
if (!function_exists("htmlspecialchars_decode"))
{
	function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT)
	{
		return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
	}
}

/*文章列表请求*/

 else if ($act == 'get_list'){
 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
	
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :20;

	$type =!empty($_REQUEST['type'])?intval($_REQUEST['type']) :0;
	
	if ($type==0)
	{
		$sql = " select a.title,a.description,a.click_count,a.article_id,a.add_time,a.file_url,ac.cat_name,ac.show_type,cr.copyright_type from " . $ecs->table('article') . ' AS a LEFT JOIN '
					 . $ecs->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id LEFT JOIN '. $ecs->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id'; 
		$sql .= " WHERE a.is_open = 1 AND (a.is_hot > 0 OR a.article_type > 0) ";
		$sql .= " ORDER BY a.add_time desc ";
		$sql .= " limit " .($pageNo-1)*$pageSize." ,".$pageSize;				
	}
	else
	{
		$sql = " select a.title,a.description,a.click_count,a.article_id,a.add_time,a.file_url,ac.cat_name,ac.show_type,cr.copyright_type from " . $ecs->table('article') . ' AS a LEFT JOIN '
					 . $ecs->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id LEFT JOIN '. $ecs->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id'; 
		$sql .= " WHERE a.is_open = 1 AND ac.show_type = $type ";
		$sql .= " ORDER BY a.add_time desc ";
		$sql .= " limit " .($pageNo-1)*$pageSize." ,".$pageSize;		
	}
			
	$row =$GLOBALS['db']->getAll($sql);
//	var_dump($row);

	$article_array = array();
	foreach ($row as $key => $article_data)
	{
		$article_array[$key]['Title'] = $article_data['title'];
		$article_array[$key]['description'] = $article_data['description'];
		$article_array[$key]['show_type'] = $article_data['show_type'];		
		$article_array[$key]['cat_name'] = $article_data['cat_name'];				
		$article_array[$key]['Id'] = $article_data['article_id'];		
		$article_data['copyright_type'] == 1 ? $article_array[$key]['is_original'] = 1 : $article_array[$key]['is_original'] = 0;				
		$article_array[$key]['click_count'] = $article_data['click_count'];				
		//文章时间
		$article_array[$key]['add_time']=local_date('Y-m-d H:i', $article_data['add_time']);		
		if ($type == 2)
		{//图赏模式，列表头图使用首张图片
			$sql = "SELECT img_url from ". $ecs->table('article_gallery'). " WHERE article_id = ".$article_data['article_id']." ORDER BY sort_order,img_id LIMIT 1 ";
			$row[$key]['file_url'] = $GLOBALS['db']->getOne($sql);
		}
//		if ($type == 3)
//		{//图赏模式，列表头图使用首张图片
//			$sql = "SELECT img_url from ". $ecs->table('article'). " WHERE article_id = ".$article_data['article_id']
//			$row[$key]['file_url'] = $GLOBALS['db']->getOne($sql);
//		}		
		$article_array[$key]['ImageUrl'] = OSS_HOST.get_image_path($article_data['article_id'],$row[$key]['file_url'],false,'artile');
//		$article_array[$key]['ImageUrl'] = getSignedUrlForGettingObject($ossClient, $bucket, OSS_HOST,get_image_path($article_data['article_id'],$article_data['file_url'],false,'artile'));
	}
	echo json_encode($article_array);
}
/*收藏文章*/
else if ($_REQUEST['act'] == 'article_mark')
{
	$aid=isset($_GET['aid']) ? $_GET['aid'] : '';
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$time = gmtime();
	$sql=" select id from".$GLOBALS['ecs']->table('article_mark')." where article_id = '$aid' and user_id = '$uid' ";
	if($GLOBALS['db']->getOne($sql)){
		$uu['error']=0;
		echo json_encode($uu);
		return;
	}else{

		$sql=" insert ".$GLOBALS['ecs']->table('article_mark')." (article_id,user_id,create_time) values ('$aid','$uid','$time')";
	}
		

	if($GLOBALS['db']->query($sql))
	{
		$uu['error']=0;
	}else
	{
		$uu['error']=1;
	}
	echo json_encode($uu);
}
/*获取我的文章收藏列表*/
else if($_REQUEST['act']=='mymark'){
	$uid=isset($_GET['uid']) ? $_GET['uid'] : '';
	$pageNo=isset($_GET['pageNo']) ? $_GET['pageNo'] : 0 ;
	$pageSize=isset($_GET['pageSize']) ? $_GET['pageSize'] : 10 ;
	$pageNo=$pageNo-1;
	$sql=" select aw.id,a.title,a.file_url,a.description,ac.cat_name,ac.show_type,a.click_count,a.article_id from ".$GLOBALS['ecs']->table('article_mark')." as aw ".
	" left join ".$GLOBALS['ecs']->table('article')." as a on a.article_id =aw.article_id ".
	" left join ". $ecs->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '.
	" where  aw.user_id = '$uid' ".
	" ORDER BY aw.create_time desc ".
	"limit ".$pageNo*$pageSize." , ".$pageSize;
	$uu=$GLOBALS['db']->getAll($sql);
//	$sql = " select a.title,a.description,a.click_count,a.article_id,a.add_time,a.file_url,ac.cat_name,ac.show_type,cr.copyright_type from " . $ecs->table('article') . ' AS a LEFT JOIN '
//				 . $ecs->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id LEFT JOIN '. $ecs->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id'; 
//	$sql .= " WHERE a.is_open = 1 AND (a.is_hot > 0 OR a.article_type > 0) ";
//	$sql .= " ORDER BY a.add_time desc ";
//	$sql .= " limit " .($pageNo-1)*$pageSize." ,".$pageSize;		
	foreach ($uu as $key => $picture)
	{
		if($uu[$key]['file_url']){
			$uu[$key]['ImageUrl'] = OSS_HOST.$picture['file_url'];
		}
//			$uu[$key]['goods_name'] = $picture['title'];
		
	}
	echo json_encode($uu); 
}
/*删除我的文章收藏*/
else if($_REQUEST['act']=='delectmymark')
{
	$id=isset($_GET['id']) ? $_GET['id'] : '';
	$sql=" delete from ".$GLOBALS['ecs']->table('article_mark')." where id ='$id' ";
	if($GLOBALS['db']->query($sql)){
		$rerult['error']=0;
	}else{
		$rerult['error']=1;
	}
	echo json_encode($rerult); 
}
/*文章内容请求*/
 else if ($act == 'get_info'){

 	$nid = !empty($_REQUEST['nid']) ? intval($_REQUEST['nid']) : '';

	if ($nid > 0)
	{
//		$article_row = $GLOBALS['db']->getrow('SELECT count(am.id) as mark_count,a.title,a.subtitle,a.description,a.click_count,a.content,a.file_url,a.link,a.author,a.add_time,a.original_content,a.copyright_id,ac.cat_name,ac.show_type FROM ' . $ecs->table('article') . ' AS a '.
//		'LEFT JOIN ' . $ecs->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '.
//		'LEFT JOIN ' . $ecs->table('article_mark') . ' AS am ON a.article_id = am.article_id '.
//		' WHERE a.article_id = ' . $nid . ' AND a.is_open = 1');		
		$article_row = $GLOBALS['db']->getrow('SELECT a.title,a.article_id,a.subtitle,a.description,a.click_count,a.content,a.file_url,a.link,a.author,a.add_time,a.original_content,a.copyright_id,ac.cat_name,ac.show_type FROM ' . $ecs->table('article') . ' AS a '.
		'LEFT JOIN ' . $ecs->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '.
		' WHERE a.article_id = ' . $nid . ' AND a.is_open = 1');		
		if (!empty($article_row))
		{
			$result['title'] = $article_row['title'];
			$result['cat_name'] = $article_row['cat_name'];			
			$result['click_count'] = $article_row['click_count'];						
			$result['subtitle'] = $article_row['subtitle'];			
			$result['author'] = $article_row['author'];
			$result['show_type'] = $article_row['show_type'];
			$result['article_id'] = $article_row['article_id'];
//			$result['mark_count'] = $article_row['mark_count'];
			$article_row['mark_count'] = 1;
			if($article_row['mark_count']){
				$result['mark_count']=$article_row['mark_count'];
				$uid = !empty($_GET['uid']) ? intval($_GET['uid']) : '';
				if($uid){
					$sql=" select id from ".$GLOBALS['ecs']->table('article_mark')." where user_id='$uid' and article_id = '$nid' ";
	
					if($GLOBALS['db']->getOne($sql)){
						$result['marked']=1;
					}else{
						$result['marked']=0;
					}
				}else{
						$result['marked']=0;
				}
			}else{
				$result['mark_count']=0;
				$result['marked']=0;
			}
			$result['is_original'] = 0;
			//获取文章版权信息
			if($article_row['copyright_id']>0)
			{
				$copyright_info = $GLOBALS['db']->getRow('SELECT * FROM ' . $ecs->table('copyright') . ' WHERE copyright_id = ' . $article_row['copyright_id']);
				$copyright_info['copyright_type'] == 1 ? $result['is_original'] = 1 : $result['is_original'] = 0;	
				$result['copyright_content'] = $copyright_info['copyright_content'];
				if ($copyright_info['copyright_replace'] >0)
				{
					$result['copyright_content'] = sprintf($result['copyright_content'],$article_row['original_content']);									
				}
				if ($copyright_info['img_url'])
				{
					$result['copyright_img'] = OSS_HOST.$copyright_info['img_url'];					
				}
			}
			//校正文章内容图片地址
			if ($article_row['show_type']==1 || $article_row['show_type']==4 || $article_row['show_type']==5)
			{
				$result['content'] = str_replace('<img src="/', '<img src="'.OSS_HOST , $article_row['content']);		
				//检测文章内容是否插入产品
				$result['content'] = insert_goods($result['content']);																																	
			}
			else if ($article_row['show_type']==2)
			{
				$article_img = $GLOBALS['db']->getAll('SELECT img_url,img_desc FROM ' . $ecs->table('article_gallery') . ' WHERE article_id = ' . $nid . ' ORDER BY sort_order,img_id');
				foreach ($article_img as $key => $img)
				{
					$article_img[$key]['img_url'] = OSS_HOST.$img['img_url'];
				}				
				$result['content'] = $article_img;																
			}						
			else if($article_row['show_type'] == 3)
			{
				$result['video_url'] = OSS_VIDEO.$article_row['link'];
				$result['video_img'] = OSS_HOST.$GLOBALS['db']->getOne('SELECT img_url FROM ' . $ecs->table('article') . ' WHERE article_id = ' . $nid);
			}
			//文章头图，供分享使用
			$result['share_img'] = OSS_HOST.get_image_path($nid,$article_row['file_url'],false,'artile');			
			$result['description'] = $article_row['description'];			
			//文章时间
			$result['add_time']=local_date('Y-m-d H:i', $article_row['add_time']);
			//文章评论总数
			$result['comment_count']=$db->getOne("SELECT count(*) FROM " . $ecs->table('article_comment') . " WHERE comment_type =1 AND id_value = '$nid'");			
			//相关商品列表
			$result['related_goods'] = article_related_goods($nid);
			//相关评测
			$result['related_eva'] = article_related_article($nid, 1, 3);			
			//相关资讯			
			$result['related_news'] = article_related_article($nid, 2, 3);						
			/* 更新点击次数 */
			$db->query('UPDATE ' . $ecs->table('article') . " SET click_count = click_count + 1 WHERE article_id = '$nid'");
		}
	}
	echo json_encode($result);

	
 }

/**
 * 文章内插入商品情况
 *
 * @access  public
 * @param   integer $content
 * @param   integer $type	1评测；2资讯
 * @return  array
 */
function insert_goods($content)
{
	$result = array();
	$part_array = explode('"></a>',$content);
//	var_dump($part_array);
	for($i=0; $i<count($part_array); $i++)
	{
		$mark_str = '<a name="';		
		$mark_pos = strpos($part_array[$i],$mark_str);
		if ($mark_pos)
		{//查找到商品替换标签
			$part_content = substr($part_array[$i],0,$mark_pos);
			$goods_id = substr($part_array[$i],$mark_pos+strlen($mark_str));
			if ($goods_id)
			{
//				$goods_id = substr($part_goods,strlen());
				$part_goods = get_goods($goods_id);
			}
			array_push($result,array("content"=>$part_content,"goods"=>$part_goods));			
		}
		else
		{
			array_push($result,array("content"=>$part_array[$i],"goods"=>""));
		}
	}
//	var_dump($result);	
	return $result;
//	if (strstr($result['content'],"<a name="))
//	{
//		$news_part;
//		$part_content = strstr($result['content'],"<a name='");
//		$part_end = strpos(strstr($result['content'],"<a name=",false),"'></a>");		
//	}
}

/**
 * 获得文章关联的文章
 *
 * @access  public
 * @param   integer $id
 * @param   integer $type	1评测；2资讯
 * @return  array
 */
function article_related_article($id, $type, $limit='')
{	
    /* 相关商品ID */
    $sql = "SELECT goods_id FROM " . $GLOBALS['ecs']->table('goods_article') . " WHERE article_id = '$id'";
    $relate_goods = $GLOBALS['db']->getAll($sql);

	if ($relate_goods)
	{
	    /* 相关文章ID */
	    $sql = "SELECT article_id FROM " . $GLOBALS['ecs']->table('goods_article') . " WHERE goods_id ".db_create_in(array_column($relate_goods,'goods_id'));
	    $row = $GLOBALS['db']->getAll($sql);	
		$article_id_arr = array_column($row, 'article_id');		
	}	
	    
    /* 指定的文章ID */
    $sql = "SELECT article_link_id FROM " . $GLOBALS['ecs']->table('article_article') . " WHERE article_id = '$id' ";
    $row = $GLOBALS['db']->getAll($sql);	
	if ($relate_goods)
	{
		$article_id_arr = array_merge($article_id_arr,array_column($row, 'article_link_id'));		
	}
	else
	{
		$article_id_arr = array_column($row, 'article_link_id');				
	}
			
    $sql = "SELECT article_id FROM " . $GLOBALS['ecs']->table('article_article') . " WHERE article_link_id = '$id' ";
    $row = $GLOBALS['db']->getAll($sql);		
	$article_id_arr = array_merge($article_id_arr,array_column($row, 'article_id'));		
	
	/* 获取文章内容 */
	$sql = " select a.title,a.description,a.click_count,cr.copyright_type,a.article_id,a.file_url as img_url,ac.cat_name,ac.show_type from " . $GLOBALS['ecs']->table('article') . ' AS a LEFT JOIN '
				 . $GLOBALS['ecs']->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '
				.' LEFT JOIN '. $GLOBALS['ecs']->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id' ; 
	$sql .= " WHERE a.is_open = 1 AND a.article_id <> '$id' AND a.article_id ".db_create_in(array_unique($article_id_arr));
	if ($type == 1) 
	{//评测
		$sql .= " AND (ac.show_type = 1 OR ac.show_type = 2 OR ac.show_type = 3) ";			
	}
	else if ($type == 2)
	{//资讯
		$sql .= " AND (ac.show_type = 4 OR ac.show_type = 5) ";					
	}
	$sql .= " ORDER BY a.add_time desc ";
    $row = $GLOBALS['db']->getAll($sql);
	if ($limit){
		$sql .= " limit " .$limit;							
	}
    $row = $GLOBALS['db']->getAll($sql);		
	
//	var_dump($row);		
	foreach ($row as $key => $article)
	{
		//文章头图
		$article['img_url'] = OSS_HOST.get_image_path($article['article_id'],$article['img_url'],false,'artile');
//		$article_array[$key]['ImageUrl'] = getSignedUrlForGettingObject($ossClient, $bucket, OSS_HOST,get_image_path($article_data['article_id'],$article_data['file_url'],false,'artile'));
		$article['copyright_type'] == 1 ? $article['is_original'] = 1 : $article['is_original'] = 0;
		$row[$key] = $article;		
	}
			
    return array('data'=>$row, 'count'=>count($row));
}

/**
 * 获得文章关联的商品
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function article_related_goods($id)
{	
    /* 相关商品 */
    $sql = "SELECT a.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods_article') . " AS a, " . $GLOBALS['ecs']->table('goods') . " AS g " .
            "WHERE a.goods_id = g.goods_id AND g.is_delete = 0 " .
            "AND a.article_id = '$id'"; //LIMIT 3
    $row = $GLOBALS['db']->getAll($sql);
	
	foreach ($row as $key => $good)
	{
		$row[$key]['level'] = '';		
		if ($good['level']>0)
		{
			$row[$key]['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']);
		}
		$row[$key]['intelligent'] = '';		
		if ($good['intelligent']>0)
		{
			$row[$key]['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']);
		}
		$row[$key]['property'] = '';
		if ($good['property']>0)
		{
			$row[$key]['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']);
		}				
		//校正图片地址
		$row[$key]['goods_img'] = OSS_HOST.get_image_path($id,$good['goods_img'],false,'goods');		

		$row[$key]['good_count'] = 10;	
		$row[$key]['price'] = (int)$good['price'];
		
	}
			
    return array('data'=>$row, 'count'=>count($row));
}

/**
 * 获得商品数据
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function get_goods($id)
{	
    /* 相关商品 */
    $sql = "SELECT g.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
            "WHERE g.goods_id = '$id' AND g.is_delete = 0 ";
    $good = $GLOBALS['db']->getRow($sql);
	
	if($good)
	{
		$good['level']>0 ? $good['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']) : $good['level'] = "";
		$good['intelligent']>0 ? $good['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']) : $good['intelligent'] = "";
		$good['property']>0 ? $good['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']) : $good['property'] = "";
		//校正图片地址
		$good['goods_img'] = OSS_HOST.get_image_path($good['goods_id'],$good['goods_img'],false,'goods');		

		$good['price'] = (int)$good['price'];
		
	}
			
    return $good;
}

?>